<?php
include('../../../includes/classes/core.php');



$start = $_REQUEST['start'];
$end   = $_REQUEST['end'];
$action = $_REQUEST['act'];
$name = $_REQUEST['name'];
$error = '';
$data = '';

function GetSum($start, $end){
$result = mysql_fetch_assoc(mysql_query("SELECT 	IFNULL(ROUND(SUM(sale_transaction_detail.product_quantity*sale_transaction_detail.cost),2),0) AS `sum`
										   FROM     sale_transaction
										   JOIN     sale_transaction_detail ON sale_transaction.id = sale_transaction_detail.transaction_id
										  WHERE     DATE(`sale_transaction`.`end_date`) >= '$start' AND DATE(`sale_transaction`.`end_date`) <= '$end' AND sale_transaction.status=0"));
return $result['sum'];
}

function GetQuantity($start, $end){
	$result = mysql_fetch_assoc(mysql_query("SELECT  IFNULL(SUM(sale_transaction_detail.product_quantity),0) AS `sum`
											   FROM  sale_transaction
											   JOIN  sale_transaction_detail ON sale_transaction.id = sale_transaction_detail.transaction_id
											  WHERE  DATE(`sale_transaction`.`end_date`) >= '$start' AND DATE(`sale_transaction`.`end_date`) <= '$end' AND sale_transaction.status=0"));
	return $result['sum'];
}

function GetProductSum($name, $start, $end){
	$result = mysql_fetch_assoc(mysql_query("SELECT 		ROUND(SUM(sale_transaction_detail.product_quantity*sale_transaction_detail.cost),2) AS `sum`
			FROM sale_transaction
			JOIN sale_transaction_detail ON sale_transaction.id = sale_transaction_detail.transaction_id
			JOIN production ON production.id = sale_transaction_detail.production_id
			JOIN production_category ON production.pos_menu_category = production_category.id
			WHERE production_category.`name` = '$name' AND production_category.`name` IS NOT NULL AND DATE(`sale_transaction`.`end_date`) >= '$start' AND DATE(`sale_transaction`.`end_date`) <= '$end' AND sale_transaction.`status` =0"));
	return $result['sum'];	
}

function GetProductQuantity($name, $start, $end){
	$result = mysql_fetch_assoc(mysql_query("SELECT SUM(sale_transaction_detail.product_quantity) AS `sum`
			FROM sale_transaction
			JOIN sale_transaction_detail ON sale_transaction.id = sale_transaction_detail.transaction_id
			JOIN production ON production.id = sale_transaction_detail.production_id
			JOIN production_category ON production.pos_menu_category = production_category.id
			WHERE production_category.`name` = '$name' AND production_category.`name` IS NOT NULL AND DATE(`sale_transaction`.`end_date`) >= '$start' AND DATE(`sale_transaction`.`end_date`) <= '$end' AND sale_transaction.`status` =0"));
	return $result['sum'];
}


switch ($action) {

	case 'get_category_sum' :
		
		$data	= array('page' => array(GetSum($start, $end),GetQuantity($start, $end)));
		break;
	case 'get_products_sum' :
		

		$sum = GetProductSum($name, $start, $end);
		$qnt = GetProductQuantity($name, $start, $end);
		
		$data	= array('page' => array($sum, $qnt));
		break;

	default :
		echo "Action is null!";
		break;

}

$data['error'] = $error;
echo json_encode($data);

?>